package com.newly.center.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newly.center.business.service.MessageService;
import com.newly.common.base.entity.business.Dto.MessageDto;
import com.newly.common.base.entity.business.Po.Message;
import com.newly.common.base.exception.NewlyException;
import com.newly.common.core.utils.PageUtil;
import com.newly.common.mapper.business.MessageMapper;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @anthor sbq
 * @date 2021/12/2 15:21
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Override
    public PageUtil getPage(MessageDto messageDto) {

        Page<Message> page = new Page<>(messageDto.getPageNum(),messageDto.getPageSize());
        LambdaQueryWrapper<Message> lq = new LambdaQueryWrapper<>();
        lq.orderByDesc(Message::getId);
        page = this.page(page,lq);
        return new PageUtil(page);
    }

    @Override
    public void changeStatus(Long[] ids) {

        List<Message> messageList = this.list(new LambdaQueryWrapper<Message>().in(Message::getId,Arrays.asList(ids)));
        if (messageList.isEmpty())
            throw new NewlyException("请检查id是否正确");
        messageList = messageList.stream().peek(p -> p.setMessageStatus(1)).collect(Collectors.toList());
        this.updateBatchById(messageList);
    }
}
